package com.fqh.controller;

import com.fqh.entity.Diningtable;
import com.fqh.entity.JsonResult;
import com.fqh.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TableController {

    @Autowired
    private TableService tableService;

    @PostMapping("/FindTableCount.do")
    public Map findTableCount() {
        Map<String, Integer> map = new HashMap();
        Integer count1 = tableService.findTableCount("空闲");
        Integer count2 = tableService.findTableCount("待结");
        map.put("空闲", count1);
        map.put("待结", count2);
        return map;
    }

    @RequestMapping("/findAllTable.do")
    public Map findAllTable(int page, int limit, String search) {
        List<Diningtable> list = tableService.findByPage(page, limit, search);
        Integer count = tableService.findTotalCount(search);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list);
        map.put("count", count);
        return map;
    }

    @RequestMapping("/deleteTableByTableId.do")
    public JsonResult deleteTableByTableId(String tableId) {
        JsonResult result = null;
        try {
            tableService.deleteTableByTableId(tableId);
            result = new JsonResult("删除成功", true);
        } catch (Exception e) {
            result = new JsonResult("删除失败", false);
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/addTable.do")
    public JsonResult addTable(String tableId) {
        JsonResult result = null;
        try {
            tableService.addTable(tableId);
            result = new JsonResult("添加成功", true);
        } catch (Exception e) {
            result = new JsonResult("添加失败", false);
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/findTableByTableId.do")
    public Diningtable findTableByTableId(String tableId) {
        return tableService.findTableByTableId(tableId);
    }

    @RequestMapping("/updateTable.do")
    public JsonResult updateTable(String tableId, String tableState) {
        JsonResult result = null;
        try {
            tableService.updateTable(tableId, tableState);
            result = new JsonResult("修改成功", true);
        } catch (Exception e) {
            result = new JsonResult("修改失败", false);
            e.printStackTrace();
        }
        return result;
    }
}
